import plugin from "../../../lib/plugins/plugin.js";
import common from "../../../lib/common/common.js";
import { existplayer, oImages, Read_json, Read_player, Write_player } from "../model/index.js";

export class uplog extends plugin {
  constructor() {
    super({
      rule: [
        {
          reg: "^#?#*创建存档$",
          fnc: "create",
        },
        {
          reg: "^#?#*查看商店$",
          fnc: "check_shop",
        },
        {
          reg: "^#?#*购买.*$",
          fnc: "buy_shop",
        },
        {
          reg: "^#?#*穿戴.*$",
          fnc: "wear",
        },
        {
          reg: "^#?#*个人信息.*$",
          fnc: "information",
        },
      ],
    });
  }
  async information(e) {
    if (!(await existplayer(e.user_id))) {
      let player = {
        战力: 0,
        迷你豆: 0,
        装备: [],
        已穿戴装备: [],
      };
      e.reply(`未创建文档，自动创建存档成功`);
      await Write_player(e.user_id, player);
    }
    await common.sleep(500);

    let player = await Read_player(e.user_id);
    let equiment = [];
    let equiment2 = [];
    player.已穿戴装备.forEach((element) => {
      equiment.push(`\n${element.name},战力${element.战力}`);
    });
    player.装备.forEach((element) => {
      equiment2.push(`\n${element.name},战力${element.战力}`);
    });
    e.reply(`
    qq:${e.user_id},
    迷你豆:${player.迷你豆}
    战力:${player.战力},
    已穿戴装备:${equiment},
    装备:${equiment2}
    `);
  }
  async create(e) {
    if (await existplayer(e.user_id)) {
      e.reply("已存在文档，无需创建");
      return;
    }
    let player = {
      战力: 0,
      迷你豆: 0,
      装备: [],
      已穿戴装备: [],
    };
    e.reply(`创建存档成功`);
    await Write_player(e.user_id, player);
  }
  async check_shop(e) {
    let json = await Read_json(1);
    console.log(json);
    let msg = [];
    json.forEach((element) => {
      msg.push(`名字${element.name},价格${element.迷你豆},战力${element.战力}\n`);
    });
    const img = await oImages("/resource/html/shop/shop.html", { json });
    e.reply(img);
  }

  async buy_shop(e) {
    if (!(await existplayer(e.user_id))) return;
    let player = await Read_player(e.user_id);
    let json = await Read_json(1);
    const [thing_name] = e.msg
      .replace(/(\/|#)?购买/, "")
      .trim()
      .split("*")
      .map((code) => code.trim());
    let thing = json.find((item) => item.name == thing_name);
    if (!thing) return e.reply(`没有这个东西`);
    if (player.迷你豆 < thing.迷你豆) return e.reply(`迷你豆不足，还需要${thing.迷你豆 - player.迷你豆}`);
    let player_thing = player.装备.find((item) => item.name == thing_name);
    player.迷你豆 -= thing.迷你豆;
    if (!player_thing) {
      player.装备.push({
        ...thing,
        数量: 1,
      });
    } else {
      player_thing.数量 += 1;
    }
    e.reply(`购买成功: ${thing.name}`);
    await Write_player(e.user_id, player);
  }
  async wear(e) {
    if (!(await existplayer(e.user_id))) return;
    let player = await Read_player(e.user_id);
    const [thing_name] = e.msg
      .replace(/(\/|#)?穿戴/, "")
      .trim()
      .split("*")
      .map((code) => code.trim());
    let player_thing = player.装备.find((item) => item.name == thing_name);
    let player_thing2 = player.已穿戴装备.find((item) => item.name == thing_name);
    if (!player_thing) return e.reply(`没有找到这个物品`);
    if (player_thing2) return e.reply(`这个物品你已经装备`);
    player.已穿戴装备.push({
      ...player_thing,
    });
    player_thing.数量 -= 1;
    player.战力 += player_thing.战力;
    player.装备 = player.装备.filter((item) => item.name !== thing_name);
    e.reply(`穿戴成功: ${player_thing.name}`);
    await Write_player(e.user_id, player);
  }
}